#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QButtonGroup>
#include <QProcess>
#include <QStringListModel>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QListView *listview = ui->listView;       //创建QListView对象

    QPushButton *btn6=ui->pushButton_6;
    QPushButton *btn7=ui->pushButton_7;

    QPushButton *psuh=ui->pushButton_2;

    QProcess process;
    QStringList arguments;
    arguments.append("devices");
    //执行ls命令
    process.start(adb ,arguments );
    //等待命令执行结束
    process.waitForFinished();
    //获取命令执行的结果
    QByteArray result_ = process.readAllStandardOutput();
    QStringList list= QString(result_).replace("\tdevice","").replace("\r","").split("\n");
    list.removeFirst();
    list.removeLast();
    list.removeLast();


    //使用数据列表创建数据显示模型
    QStringListModel *listmodel = new QStringListModel(list);
    listview->setModel(listmodel);                   //设置模型到listview上

    listview->setMovement(QListView::Free);          //设置数据可以自由拖动
    listview->setSpacing(2);                         //设置数据的间距

    connect(listview, SIGNAL(clicked(QModelIndex)), this, SLOT(slotClicked(QModelIndex)));

    connect(btn6, SIGNAL(clicked()), this, SLOT(slotClicked6()));
    connect(btn7, SIGNAL(clicked()), this, SLOT(slotClicked7()));

    connect(psuh, SIGNAL(clicked()), this, SLOT(psuhClicked()));

    {
         groupButton111 = new QButtonGroup(this);
           groupButton111->addButton(ui->radioButton_1,0);
           groupButton111->addButton(ui->radioButton_2,1);
           groupButton111->addButton(ui->radioButton_3,2);
           ui->radioButton_1->setChecked(true);

           connect(ui->radioButton_1,SIGNAL(clicked(bool)), this,SLOT(slots_arch()));
           connect(ui->radioButton_2,SIGNAL(clicked(bool)), this,SLOT(slots_arch()));
           connect(ui->radioButton_3,SIGNAL(clicked(bool)), this,SLOT(slots_arch()));

    }

    {
           groupButton222=new QButtonGroup(this);
           groupButton222->addButton(ui->radioButton_4,0);
           groupButton222->addButton(ui->radioButton_5,1);

           ui->radioButton_4->setChecked(true);

           connect(ui->radioButton_4,SIGNAL(clicked(bool)), this,SLOT(slots_out_type()));
           connect(ui->radioButton_5,SIGNAL(clicked(bool)), this,SLOT(slots_out_type()));

    }

    connect(&timer, SIGNAL(timeout()), this, SLOT(listDevices()));
    timer.start(1000);


    connect(ui->installapk,SIGNAL(clicked()), this,SLOT(slots_install()));

    ui->lineEdit->setText(projBase);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slotClicked(const QModelIndex &index)
{

    device = index.data().toString();
    qDebug()<<device;

}
// 不带端口号的IP地址
QString getIpAddress(const QString & strText)
{
    QStringList strList;

    QRegExp rx("(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)");

    QString strRemain = strText;

    int nPos = rx.indexIn(strRemain);
    int nLenth = rx.matchedLength();

    while (nPos >= 0 && nLenth > 0)
    {
        strList.push_back(strRemain.mid(nPos, nLenth));

        strRemain = strRemain.mid(nPos + nLenth, strRemain.length() - (nPos + nLenth));
        nPos = rx.indexIn(strRemain);
        nLenth = rx.matchedLength();
    }
    qDebug()<<strList;


    for (int i=0;i<strList.size();i++){
        QString s = strList.takeAt(i);
        if(s.contains("192.168")||s.contains("172.16"))return s;
    }

   // return strList;
}
void MainWindow::slotClicked6()
{
    qDebug()<<"slotClicked6";
    QStringList list;

    QString cmd=adb+ " -s "+device+" shell ifconfig";
    QString s = execSysCmd(cmd,list);
    qDebug()<<s;
    qDebug()<<getIpAddress(s);


}

void MainWindow::slotClicked7()
{
     qDebug()<<"slotClicked7";

}
QString MainWindow::execSysCmd(QString p,QStringList args) {
     QProcess process;
     //执行ls命令
     if( args.size()==0)
     {
         process.start(p);
     }else{
         process.start(p,args);
     }

     //等待命令执行结束
     process.waitForFinished();
     //获取命令执行的结果
     QByteArray result_ = process.readAllStandardOutput();
     return QString(result_.data());

}

//void on_adb_devices_clicked()
QString MainWindow::on_pushButtonD_clicked()
{
     qDebug("on_pushButtonD_clicked");
    //QProcess用于启动外部程序
    QProcess process;
    //执行ls命令
    process.start("/home/kali/Android/Sdk/platform-tools/adb devices");
    //等待命令执行结束
    process.waitForFinished();
    //获取命令执行的结果
    QByteArray result_ = process.readAllStandardOutput();
    QStringList list= QString(result_).split("\n");

    //打印结果
    qDebug(result_);
    return QString(result_);
}

void MainWindow::slots_arch()
{

    switch(groupButton111->checkedId())
    {
        case 0:
            soArch="arm64-v8a";

            break;
        case 1:
            soArch = "armeabi-v7a";
            break;
        case 2:

            soArch="x86";
            break;


    }
    qDebug() <<soArch;
}

void MainWindow::slots_out_type()
{

    switch(groupButton222->checkedId())
    {
        case 0:
            out="debug";

            break;
        case 1:
            out = "realase";

            break;



    }
    qDebug() <<out;
}

void MainWindow::psuhClicked() {
    qDebug("psuhClicked");
    QString cmd = "adb -s "+device+" push "+projBase+"/app/build/intermediates/stripped_native_libs/"+out+"/out/lib/"+soArch+"/libacc.so /sdcard/Android/data/"+pkg+"/files/ghplugin/com.gh.plugin.main/lib/libacc.so";
    qDebug()<<cmd;
    QStringList tmp;
    execSysCmd(cmd,tmp)   ;
}

void MainWindow::slots_install() {

}

void MainWindow::listDevices() {

    QStringList tmp;
    {
             QString cmd = "adb devices";
            QString ret= execSysCmd(cmd,tmp);
            QStringList  list= QString(ret).replace("\tdevice","").split("\n");
            list.removeFirst();
            list.removeLast();
            list.removeLast();

            if (device.isEmpty())device=list.first();

          //  qDebug()<<list;
    }

    {

            //    adb -s emulator-5556 shell "dumpsys window | grep mCurrentFocus"
            QString cmd="adb -s "+device+" shell \"dumpsys window | grep mCurrentFocus\"";
            QString str= execSysCmd(cmd,tmp);
            QStringList list= str.split(" ");
            QString d =(list.last().split("/")).first();
        //    qDebug()<<d;
            pkg=d;
    }


}


void on_pushButtonA_clicked()

{

    qDebug("pushButtonConnect");

    //QProcess用于启动外部程序
    QProcess process;
    //执行ls命令
    process.start("/home/kali/Android/Sdk/platform-tools/adb devices");
    //等待命令执行结束
    process.waitForFinished();
    //获取命令执行的结果
    QByteArray result_ = process.readAllStandardOutput();
    //打印结果
    qDebug(result_);



}
